'''
      ocean wave time series from Percival & Walden, figure 295.
      source: Applied Physics Laboratory (Andy Jessup)

      This example computes an adaptive multi-taper (kind == 2) result
      that should be compared to the heavy curve in P&W figure 373(c).
''' 

import sys
import pymutt
import matplotlib.pyplot as mpl
import numpy as np
from utilities import mtanalyze


dt = 0.25
dtunits = "second"
data = [
    477,
    483, 507, 512, 474, 402, 335, 295, 283, 275, 272, 279,
    311, 339, 325, 260, 196, 185, 233, 301, 353, 399, 466,
    540, 592, 624, 645, 661, 659, 631, 587, 530, 451, 328,
    175, 28, -64, -99, -103, -113, -157, -220, -290, -352, -370,
    -327, -224, -88, 55, 201, 366, 543, 709, 830, 890, 884,
    791, 633, 434, 225, 46, -74, -121, -115, -94, -95, -129,
    -183, -230, -237, -205, -149, -96, -40, 36, 137, 261, 401,
    560, 730, 887, 983, 991, 927, 836, 759, 696, 613, 477,
    279, 43, -188, -356, -436, -431, -364, -260, -139, -9, 119,
    237, 357, 480, 597, 682, 701, 648, 531, 393, 264, 141,
    15, -103, -193, -236, -239, -205, -127, -9, 125, 244, 322,
    376, 416, 457, 511, 571, 634, 684, 719, 735, 735, 718,
    670, 594, 490, 381, 279, 179, 87, -1, -84, -172, -264,
    -322, -304, -217, -96, 43, 193, 348, 466, 522, 517, 491,
    479, 501, 561, 638, 710, 743, 724, 644, 520, 368, 198,
    36, -101, -195, -248, -289, -322, -348, -345, -308, -229, -105,
    59, 238, 365, 427, 450, 480, 540, 606, 657, 693, 725,
    763, 783, 780, 751, 703, 645, 573, 494, 395, 267, 99,
    -78, -230, -321, -316, -224, -80, 65, 176, 241, 262, 247,
    218, 214, 250, 319, 385, 432, 467, 481, 454, 394, 342,
    327, 333, 327, 281, 208, 131, 61, 7, -14, -6, 12,
    7, -35, -87, -97, -43, 53, 135, 174, 176, 181, 212,
    263, 323, 391, 459, 497, 479, 410, 308, 204, 103, 5,
    -87, -147, -125, 6, 211, 436, 636, 813, 963, 1072, 1115,
    1093, 1007, 871, 710, 553, 412, 272, 117, -39, -176, -277,
    -337, -347, -306, -236, -165, -108, -67, -15, 69, 191, 346,
    498, 604, 644, 613, 549, 465, 347, 184, 0, -159, -255,
    -286, -264, -196, -92, 32, 170, 319, 467, 593, 683, 743,
    794, 856, 912, 946, 951, 919, 833, 674, 454, 202, -43,
    -247, -387, -471, -507, -529, -567, -631, -728, -853, -964, -996,
    -918, -744, -520, -277, -26, 222, 444, 624, 751, 813, 809,
    749, 642, 494, 307, 103, -94, -268, -380, -412, -359, -274,
    -201, -165, -168, -179, -142, -5, 240, 560, 905, 1234, 1491,
    1627, 1639, 1542, 1374, 1161, 924, 685, 472, 275, 86, -94,
    -235, -312, -350, -383, -400, -366, -267, -136, -3, 138, 298,
    482, 680, 868, 1021, 1120, 1143, 1079, 947, 767, 558, 335,
    120, -52, -193, -295, -355, -353, -285, -180, -75, -8, 24,
    52, 107, 203, 334, 469, 563, 598, 572, 512, 444, 380,
    309, 222, 115, -3, -113, -187, -218, -229, -258, -316, -388,
    -445, -483, -509, -516, -488, -404, -272, -108, 63, 217, 333,
    409, 455, 473, 458, 398, 304, 204, 133, 104, 105, 133,
    174, 214, 248, 278, 304, 324, 345, 357, 372, 399, 459,
    563, 692, 808, 872, 869, 801, 677, 511, 313, 123, -13,
    -40, 58, 248, 453, 592, 633, 595, 513, 430, 366, 328,
    308, 293, 281, 278, 283, 283, 279, 287, 322, 378, 420,
    428, 393, 360, 325, 263, 174, 91, 58, 88, 141, 169,
    169, 159, 152, 139, 105, 60, 26, 32, 84, 141, 177,
    191, 211, 265, 340, 405, 425, 394, 318, 238, 197, 210,
    251, 285, 280, 195, 14, -212, -380, -393, -255, -37, 187,
    373, 505, 578, 590, 545, 460, 347, 217, 85, -19, -60,
    -40, -2, -17, -102, -207, -248, -187, -44, 135, 295, 423,
    506, 568, 620, 672, 721, 757, 773, 772, 756, 733, 697,
    626, 497, 323, 139, -31, -191, -377, -578, -738, -791, -716,
    -539, -308, -93, 64, 154, 214, 271, 332, 378, 384, 341,
    253, 149, 51, -21, -54, -52, -43, -76, -174, -295, -390,
    -441, -463, -451, -409, -327, -232, -118, 28, 209, 403, 553,
    624, 618, 550, 431, 280, 135, 26, -44, -85, -87, -29,
    77, 197, 290, 352, 401, 452, 507, 563, 593, 592, 555,
    491, 415, 346, 297, 276, 245, 180, 78, -25, -89, -101,
    -67, 4, 94, 210, 333, 432, 473, 465, 419, 360, 306,
    286, 333, 421, 501, 525, 497, 435, 365, 303, 261, 230,
    201, 166, 105, 24, -69, -155, -221, -268, -284, -263, -195,
    -85, 55, 207, 349, 470, 559, 623, 654, 652, 628, 575,
    504, 416, 327, 263, 224, 169, 69, -72, -195, -236, -181,
    -64, 75, 213, 346, 467, 572, 668, 753, 825, 864, 849,
    774, 647, 493, 344, 226, 140, 60, -43, -155, -243, -265,
    -206, -90, 58, 220, 398, 577, 725, 819, 865, 863, 809,
    699, 542, 372, 216, 71, -63, -155, -171, -120, -45, 9,
    24, 4, -56, -140, -224, -269, -248, -159, -25, 116, 241,
    349, 445, 515, 535, 479, 351, 170, -17, -158, -222, -210,
    -140, -66, 0, 43, 57, 39, -10, -105, -234, -352, -399,
    -360, -280, -210, -171, -147, -106, -26, 104, 264, 408, 495,
    503, 435, 322, 169, -9, -178, -282, -296, -246, -183, -153,
    -173, -215, -252, -270, -272, -244, -196, -133, -83, -53, -32,
    -2, 65, 179, 299, 388, 415, 372, 264, 120, -5, -70,
    -70, -19, 60, 170, 304, 449, 587, 725, 869, 1013, 1131,
    1196, 1200, 1144, 1020, 849, 675, 517, 389, 307, 291, 348,
    443, 534, 600, 645, 689, 727, 737, 715, 674, 632, 582,
    508, 415, 344, 333, 392, 477, 527, 513, 428, 306, 181,
    69, -31, -115, -173, -172, -114, -3, 118, 216, 272, 272,
    230, 157, 79, 15, -44, -120, -212, -291, -336, -336, -307,
    -257, -194, -121, -51, 0, 35, 53, 55, 57, 76, 115,
    145, 114, 10, -124, -228, -272, -251, -165, -37, 109, 219,
    263, 238, 167, 80, 10, -24, -3, 67, 157, 238, 314,
    405, 514, 600, 621, 568, 459, 325, 186, 59, -58, -158,
    -236, -267, -244, -198, -154, -125, -75, 1, 105, 232, 376,
    520, 637, 709, 733, 719, 675, 597, 500, 399, 296, 164,
    -1, -162, -277, -320, -303, -247, -177, -116, -63, -20, 19,
    79, 163, 275, 398, 511, 589, 628, 628, 604, 557, 491,
    401, 289, 181, 131, 181, 312, 457, 533, 495, 351, 155,
    -25, -138, -179, -165, -126, -78, -18, 71, 193, 329, 449,
    539, 595, 620, 588, 500, 372, 242, 137, 73, 43, 23,
    15, 14, 6, -25, -88, -167, -239, -279, -270, -210, -113,
]

def doit(verbose = 0):

    print __doc__

    a = np.array(data)
    r = mtanalyze(a - a.mean(),
                  dt = dt,
                  kind = 2,
                  npi = 4,
                  nwin = 10,
                  padby = 0,
                  doplot = 1,
                  verbose = verbose,
                  title = "Adaptive Multi-taper of Ocean Wave Data",
                  )

    return r
